package com.demo.mqtt.common.fromResource.entity;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.demo.mqtt.common.fromResource.cons.RoleConstant;
import com.demo.mqtt.common.fromResource.cons.UserConstant;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  用户
 * </p>
 *
 * @author gobigg
 * @since  2020/7/21
 */
@Builder
@AllArgsConstructor
@Data
@JsonInclude(JsonInclude.Include.ALWAYS)
public class SysUser implements UserDetails {
    private static final long serialVersionUID = 1L;

    public SysUser () {
        this.setRoleName("");
        this.setEmail("");
    }
    /**
     *  用户id
     */
    @TableId(type = IdType.UUID)
    private String id;

    /**
     * 用户昵称
     */
    private String nickName = "";

    /**
     *  用户名
     */
    private String username;

    /**
     *  用户密码
     */
    private String password;

    /**
     * 姓名
     */
    private String fullName;
    /**
     * 电话
     */
    private String mobile;
    /**
     * 邮箱
     */
    private String email;
    /**
     * 电话报警
     */
    private String mobileAlert;
    /**
     * 微信- openid
     */
    private String openId;

    /**
     * 角色名称
     */
    @TableField(exist = false)
    private String roleName;

    /**
     * security生成的token
     */
    @TableField(exist = false)
    private String token;
    /**
     * 邮箱报警
     */
    private String emailAlert;

    private Long tenantId;

    /**
     *  角色列表
     */
    @TableField(exist = false)
    private List<SysRole> authorities = new ArrayList<>();

    /**
     *  指示是否未过期的用户的凭据(密码),过期的凭据防止认证 默认true 默认表示未过期
     */
    @TableField(exist = false)
    private boolean credentialsNonExpired = true;

    /**
     *    账户是否未过期,过期无法验证 默认true表示未过期
     */
    @TableField(exist = false)
    private boolean accountNonExpired = true;

    /**
     *     用户是未被锁定,锁定的用户无法进行身份验证 默认true表示未锁定
     */
    @TableField(exist = false)
    private boolean accountNonLocked = true;

    @TableField(exist = false)
    private boolean isNew = false;

    /**
     * 是否可用 ,禁用的用户不能身份验证 默认true表示可用
     */
    @TableField(exist = false)
    private boolean enabled = true;

    /**
     * 1.系统管理员
     * 2.系统用户
     * 3.租户管理员
     * 4.租户用户
     */
    @TableField(exist = false)
    private Long roleNum;

    /**
     * 1.系统用户
     * 2.租户用户
     */
    @TableField(exist = false)
    private Long isSysUser;

    @TableField(exist = false)
    private JSONObject auth;

    @Override
    public List<SysRole> getAuthorities() {
        return authorities;
    }

    public void setCredentialsNonExpired(boolean credentialsNonExpired) {
        this.credentialsNonExpired = credentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    public boolean isAdmin() {
        if (ObjectUtils.isEmpty(this.getAuthorities()) || this.getAuthorities().size() == 0) {
            return false;
        } else {
            return this.username.equals(UserConstant.USER_ADMIN)
                    ||
                    this.getAuthorities()
                            .stream()
                            .anyMatch(s -> s.getAuthority().equals(RoleConstant.ROLE_ADMIN)
                                    || (ObjectUtils.isEmpty(s.getTenantId()) && s.getIsAdmin()));
        }
    }

    public boolean isTenantAdmin() {
        if (ObjectUtils.isEmpty(this.getAuthorities()) || this.getAuthorities().size() == 0) {
            return false;
        } else {
            return this.getAuthorities().stream().filter(sysRole -> sysRole.getIsAdmin() && !ObjectUtils.
                    isEmpty(sysRole.getTenantId())).collect(Collectors.toList()).size() > 0;
        }
    }

    public String getOpenId() {
        return openId;
    }

    public void setOpenId(String openId) {
        this.openId = openId;
    }

    public boolean isNew() {
        return this.isNew;
    }
}

